import 'package:aihu_getx_app/app/interceptors/login_intercept_thread_manager.dart';
import 'package:aihu_getx_app/app/modules/commons/commons.dart';
import 'package:aihu_getx_app/utils/cache_keys.dart';
import 'package:aihu_getx_app/utils/constants.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:window_manager/window_manager.dart';

class DHomeController extends GetxController with WindowListener {
  final appName = "".obs;
  final pageTitle = "".obs;
  final PageController pageController = PageController();

  final count = 0.obs;

  final currentIndex = 0.obs;
  late final Map? parameters;

  Widget bottomNavigationBar() {
    return Obx(
      () => BottomNavigationBar(
        currentIndex: currentIndex.value,
        onTap: changePage,
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.category),
            label: '工作台',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.chat),
            label: '消息',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我的',
          ),
        ],
      ),
    );
  }

  void changePage(index) {
    changePageIndex(index);
    if (pageController.hasClients) {
      pageController.jumpToPage(index);
    }
  }

  void changePageIndex(index) {
    LogUtil.v(
        'currentIndex.value: ${currentIndex.value}, changePageIndex: $index');
    if (currentIndex.value == index) {
      LogUtil.d("重复点击$index");
      return;
    }
    currentIndex.value = index;
    SpUtil.putInt(CacheKeys.dLastPageIndex, index);
    LoginInterceptThreadManager.get().checkLogin(() {});
  }

  @override
  void onInit() {
    super.onInit();
    windowManager.addListener(this);
    parameters = Get.arguments;
    if (parameters != null) {
      LogUtil.v("Get.arguments: $parameters");
      var jump = parameters?[Constants.pageIndexArgument];
      if (jump != null) {
        SpUtil.putInt(CacheKeys.dLastPageIndex, jump);
      }
    }
    appName.value = Commons.getCurrentClientConfig().title;
  }

  @override
  void onReady() {
    super.onReady();
    changePage(SpUtil.getInt(CacheKeys.dLastPageIndex) ?? 0);
  }

  @override
  void onClose() {
    super.onClose();
    windowManager.removeListener(this);
  }

  @override
  void onWindowResized() {
    super.onWindowResized();
    Get.forceAppUpdate();
  }

  @override
  void onWindowEnterFullScreen() {
    super.onWindowEnterFullScreen();
    Get.forceAppUpdate();
  }

  @override
  void onWindowLeaveFullScreen() {
    super.onWindowLeaveFullScreen();
    Get.forceAppUpdate();
  }

  @override
  void onWindowMaximize() {
    super.onWindowLeaveFullScreen();
    Get.forceAppUpdate();
  }

  @override
  void onWindowUnmaximize() {
    super.onWindowLeaveFullScreen();
    Get.forceAppUpdate();
  }

  void increment() => count.value++;
}
